package com.bdrm.service.impl;

import com.bdrm.constant.MessageConstant;
import com.bdrm.constant.PasswordConstant;
import com.bdrm.constant.StatusConstant;
import com.bdrm.context.BaseContext;
import com.bdrm.dto.EmployeeDTO;
import com.bdrm.dto.EmployeeLoginDTO;
import com.bdrm.dto.EmployeePageQueryDTO;
import com.bdrm.dto.PasswordEditDTO;
import com.bdrm.entity.Employee;
import com.bdrm.exception.AccountLockedException;
import com.bdrm.exception.AccountNotFoundException;
import com.bdrm.exception.PasswordEditFailedException;
import com.bdrm.mapper.EmployeeMapper;
import com.bdrm.result.PageResult;
import com.bdrm.service.EmployeeService;
import com.bdrm.vo.EmployeeLoginVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        // 获取用户名和密码
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        // 查询数据库是否有该用户
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null){
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())){
            throw new AccountNotFoundException(MessageConstant.PASSWORD_ERROR);
        }

        // 判断账号是否被锁定
        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);

        // 设置状态属性,添加及启用
        employee.setStatus(StatusConstant.ENABLE);

        // 设置默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employeeMapper.insert(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // 开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);
    }

    @Override
    public Employee getByIdUser(Long id) {
        return employeeMapper.getByIdUse(id);
    }

    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();
        employeeMapper.update(employee);
    }

    @Override
    public void pwdEdit(PasswordEditDTO passwordEditDTO) {
        String oldPassword = passwordEditDTO.getOldPassword();
        String newPassword = passwordEditDTO.getNewPassword();

        // 获取员工id
        Long empId = BaseContext.getCurrentId();
        Employee employee = employeeMapper.getByIdUse(empId);

        String password= DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!password.equals(employee.getPassword())){
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_ERROR);
        }

        // 正确就修改密码
        Employee pw = Employee.builder()
                .id(empId)
                .password(DigestUtils.md5DigestAsHex(newPassword.getBytes()))
                .build();
        employeeMapper.update(pw);
    }
}
